home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17968 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: nntp.teleport.com!usenet
  2. From: GHouck <hksys@teleport.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: File I/O using fwrite()
  5. Date: 18 Apr 1996 07:50:01 GMT
  6. Organization: systems hk
  7. Message-ID: <4l4s79$r1v@nadine.teleport.com>
  8. References: <31758D70.1D38@vixa.voyager.net>
  9. NNTP-Posting-Host: ip-pdx07-11.teleport.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. David Wade <dwade@vixa.voyager.net> wrote:
  16. >HELP!!!  I am having a major problem getting random access file routines 
  17. >to work properly.  For some reason, no matter what I do the information 
  18. >is always written at the end of the file instead of where I want it to 
  19. >replace...  All I need to do is update a record in a potentailly large 
  20. >file, and I can't load the entire file and re-write it to do it.  If 
  21. >anyone sees what I am doing wrong, or has a possible solution using 
  22. >other means, I would greatly appreciate the info.  The funtion looks 
  23. >like this:
  24. >        f=_fsopen(".\\data\\weapons.dat", "ab+", SH_DENYNONE);
  25. >        if(f == NULL)
  26. >        {
  27. [snip]
  28. >        };
  29. >        rewind(f);
  30. >        fseek(f,(item_data.reference-1)*sizeof(item_struct),SEEK_SET);
  31. >        fwrite(&item_data, sizeof(item_struct), 1, f);
  32. >        fclose(f);
  33. >
  34. >
  35. >The struct is of type item_struct, and item_data is an instance of that 
  36. >struct.
  37.  
  38. If I were to hazard a guess, I'd say that since fseek wants a long
  39. type in the offset position, and your product probably does not 
  40. result in a long value, it might be the problem.  Why keep rewinding
  41. the file as well, since it probably results in extra i/o?
  42. If your variable (.reference) is not long, try:
  43.  
  44.   fseek(f,(long)(item_data.reference-1)*sizeof(item_struct),SEEK_SET);
  45.  
  46. Yours, Geoff Houck
  47.  
  48.